package com.share.common.utils;

import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CollectionUtil {

    /**
     * 把list切割为 多个list，每个list最多splitSize各元素
     *
     * @param list
     * @param splitSize
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> splitList(List<T> list, int splitSize) {
        if (list == null || list.isEmpty()) {
            return new LinkedList<>();
        }
        if (splitSize < 1) {
            throw new RuntimeException("batchSize参数异常");
        }

        int maxSize = (list.size() + splitSize - 1) / splitSize;
        //开始分割
        return Stream.iterate(0, n -> n + 1).limit(maxSize).parallel().map(a -> list.parallelStream().skip(a * splitSize).limit(splitSize).collect(Collectors.toList())).filter(b -> !b.isEmpty()).collect(Collectors.toList());
    }

    /**
     * 切割为threadNum个单位
     *
     * @param source
     * @param threadNum
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> splitByThreadNum(List<T> source, int threadNum) {
        List<List<T>> result = new LinkedList<>();

        //参数校验
        if (source == null && source.size() < 1) {
            return result;
        }
        if (threadNum < 1) {
            throw new RuntimeException("threadNum参数异常");
        }

        int remainder = source.size() % threadNum;  //(先计算出余数)
        int number = source.size() / threadNum;  //然后是商
        int offset = 0;//偏移量
        for (int i = 0; i < threadNum; i++) {
            List<T> value;
            if (remainder > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remainder--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }

    public static void main(String[] args) {
        int size = 100;
        List<Integer> list = new LinkedList<>();
        for (int i = 0; i < size; i++) {
            list.add(i);
        }

        List<List<Integer>> lists = splitList(list, 50);
        for (List<Integer> ls : lists) {
            String aa = String.join("-", ls.stream().map(String::valueOf).collect(Collectors.toList()));
            System.out.println(ls.size() + ":" + aa);
        }
    }

    /**
     * 从map中随机取出一个key
     *
     * @param map
     * @return
     */
    public static String randomFromKey(Map<String, String> map) {
        if (CollectionUtils.isEmpty(map)) {
            return null;
        }
        Set<String> sets = map.keySet();
        return randomFromKey(sets);
    }

    public static String randomFromKey(Collection<String> collection) {
        if (CollectionUtils.isEmpty(collection)) {
            return null;
        }

        String res = null;
        int i = 0;
        int desi = UUIDUtil.genRandomInt(collection.size());
        for (String str : collection) {
            if (i == desi) {
                res = str;
                break;
            }
            i++;
        }
        return res;
    }

    //转换为数组初始化，转换为list
    public static <T> List<T> array2List(T... arr) {
        List<T> reList = null;
        if (arr == null || arr.length == 0) {
            reList = new LinkedList<>();
        } else {
            reList = Arrays.asList(arr);
        }
        return reList;
    }
}
